﻿#include "extractorLocationVector.h"
#include "grid.h"


using namespace cv;
using namespace std;

extractorLocationVector::extractorLocationVector(char* _labelImage, Slic _clustering){
	this->size = _clustering.getCenter_counts().size();
	this->labelImage = _labelImage;
	Mat clusters = _clustering.convertClustersToMat();
	int _nB = 8;
	grid gridforcluster(clusters,_nB,_labelImage);
	double sum = 0.0;
	
	// running cross clusters
	for(int icluster = 0; icluster < size; icluster++){
		locationVector _vector(_nB*_nB,icluster,_labelImage);

		for(int igrid = 0; igrid < (_nB *_nB);igrid++){
			gridItem _gridItem = gridforcluster.getItem(igrid);
			
			for(int i = 0; i < clusters.rows; i++){

				for(int j = 0 ; j < clusters.cols ; j++){

					int value_cluster = (int)clusters.at<int>(i,j);
					if(value_cluster == icluster){
						if(_gridItem.checkPixelInBound(i,j)){
							sum = sum + 1.0;
						}
					}				
				}
			}

			double ratio = sum / (double)(_gridItem.getWidth() * _gridItem.getHeight());
			if(ratio > 1){
				cout << "Yield of square: " <<(double)(_gridItem.getWidth() * _gridItem.getHeight()) << endl;
				cout << "Number of pixel: " << sum << endl;
				cout << "At cluster: " << icluster << endl;
				cout << "At square: " << igrid << endl;
				cout << "x BoundX y BoundY: " << _gridItem.getX()<< " " << _gridItem.getBoundX()<< " " << _gridItem.getY()<< " " << _gridItem.getBoundY()<< endl;
				cout << "cheking...." << endl;
				cout << endl;
			}
			_vector.addValue(ratio);
			sum = 0.0;
			
		}
		this->listVector.push_back(_vector);
	}
}

vector<locationVector> extractorLocationVector::getListVector() {
	return this->listVector;
}